﻿/*
 * @(#)SearchIt	gadget 24/01/07
 * Copyright 2007  Hemakumar M. All rights reserved.
 */


var httpReq = null;
var showing = false;

var engineIndex = 0;
var logos = ["icons\\google.ico","icons\\yahoo.ico","icons\\ebay.ico","icons\\answers.ico","icons\\cc.ico","icons\\msn.ico","icons\\altavista.ico","icons\\looksmart.ico","icons\\about.ico"];

var urls = [["Google","http://www.google.com/search?hl=en&q=${searchtext}"],
            ["Yahoo","http://search.yahoo.com/search?p=${searchtext}&ei=UTF-8"],
            ["Ebay","http://search.ebay.com/search/search.dll?from=R40&satitle=${searchtext}"],
	    ["Answers.com","http://www.answers.com/main/ntquery?s=${searchtext}&gwp=13"],
            ["Creative Commons","http://search.creativecommons.org/?q=${searchtext}"],
            ["MSN Live search","http://search.msn.co.in/results.aspx?q=${searchtext}&mkt=en-IN&form=QBRE"],
            ["Altavista","http://www.altavista.com/web/results?q=${searchtext}"],
            ["LookSmart","http://search.looksmart.com/p/search?qt=${searchtext}"],
            ["About.com","http://search.about.com/fullsearch.htm?terms=${searchtext}"]]
            //["Answers.com","http://www.answers.com/main/startswith?s=${searchtext}&method=22"],
var itemImage = new Array();

function LoadIcons ()
{
  for (var i = 0; i < logos.length; ++i)
    itemImage[i] = graphics.loadImage(logos[i]);
}


function view_onOpen(){
  //LoadIcons();
  btn_show.image = logos[0];
  btn_show.downImage = logos[0]; 
  btn_show.overImage = logos[0]; 

  txt_search.value = "";
  ShowSearchEngines();
}

function ShowSearchEngines() {
  
  btn_show.visible = false;//hide the button
  div_dropdown.visible = true;//show the search engines div
  div_dropdown.width = logos.length * 19;//set the width of the div
  div_dropdown.x = div_dropdown.x - div_dropdown.width; //set the x position
  txt_search.visible = false;//hide the search text field
  img_bk.visible = false;
  //Remove added elements (search icons)
  for(var j=0;j<elements.length;j++)
    div_dropdown.removeElement(elements[j]);
  //Add New elements (search icons)
  for(var i=0;i<logos.length;i++){
     var elem = div_dropdown.appendElement('<button height="17" onclick="HideSearchEngines('+i+')" width="16" x="'+(i*19)+'" y="1" />');
     elem.image = logos[i]; 
     elem.downImage = logos[i]; 
     elem.overImage = logos[i]; 
     elem.tooltip = urls[i][0];
     elements[i] = elem;
  } 
}

var elements = new Array();
function HideSearchEngines(ind) { 
  if(! ind) 
    ind = 0;

  engineIndex = ind;
  btn_show.visible = true;//show button
  div_dropdown.visible = false;//hide search engines div
  div_dropdown.x = 176;
  div_dropdown.width = 17

  txt_search.visible = true;
  img_bk.visible = true;

  btn_show.image = logos[ind];
  btn_show.downImage = logos[ind]; 
  btn_show.overImage = logos[ind]; 
  btn_show.tooltip = urls[ind][0];
  
  showing = !showing;
}



function Search() {
  
  if (event.keyCode == 27)
      txt_search.value="";
  else if(event.keyCode == 13){
    var searchText = new String(txt_search.value);
    searchText = searchText.replace(/^\s+|\s+$/, '');
    if(searchText!=""){
      var searchUrl = urls[engineIndex][1];
      var su = new String(searchUrl);
      su = su.replace(/\$\{searchtext\}/,searchText);
      LaunchBrowser(su);
    }
  } 
}


function LaunchBrowser(url) {
  try {
    
    var run = new ActiveXObject("Shell.Application");
    run.ShellExecute("iexplorer",url);
  } catch (e) {
    return;
  }
}
